package com.bjx.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.common.core.exception.BJXException;
import com.bjx.system.domain.entity.RoleEntity;
import com.bjx.system.domain.query.RoleMenuQuery;
import com.bjx.system.domain.query.RoleQuery;
import com.bjx.system.domain.vo.RoleMenuVO;
import com.bjx.system.domain.vo.RoleVO;
import com.bjx.system.mapper.RoleMapper;
import com.bjx.system.service.IMenuService;
import com.bjx.system.service.IRoleMenuService;
import com.bjx.system.service.IRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc: 角色服务实现
 * @author: liushuai
 * @date: 2022/10/11
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements IRoleService {
    @Resource
    private RoleMapper roleMapper;
    
    @Resource
    private IMenuService menuService;
    
    @Resource
    private IRoleMenuService roleMenuService;

    @Override
    public IPage<RoleVO> listRolePage(RoleQuery roleQuery) {
        IPage<RoleVO> iPage = roleMapper.listRolePage(roleQuery.getPage(),roleQuery);
        return iPage;
    }

    
    @Override
    public void insertRole(RoleVO roleVO) {
        RoleEntity entity = Convert.convert(RoleEntity.class, roleVO);
        roleMapper.insert(entity);
    }


    @Override
    public void updateRole(RoleVO roleVO) {
        RoleEntity entity = Convert.convert(RoleEntity.class, roleVO);
         roleMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRoleByRoleId(Long roleId) {
        roleMapper.deleteById(roleId);
        roleMenuService.deleteRoleMenuByRoleId(roleId);
    }

    @Override
    public RoleVO selectRoleByRoleId(Long roleId) {
        RoleEntity entity = roleMapper.selectById(roleId);
        RoleVO vo = Convert.convert(RoleVO.class,entity);
        return vo;
    }

    @Override
    public RoleMenuVO selectRoleMenuVOByRoleId(Long roleId) {
        RoleMenuVO roleMenuVO = new RoleMenuVO();
        List<Tree<String>> trees = menuService.listMenuAuthTree();
        roleMenuVO.setMenuAuthAll(trees);
        List<String> menuIds = roleMenuService.listRoleMenuIdSon(roleId);
        roleMenuVO.setMenuIds(menuIds);
        return roleMenuVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRoleMenu(RoleMenuQuery roleMenuQuery) {
        if(ObjectUtil.isEmpty(roleMenuQuery.getRoleId())){
            throw new BJXException("角色Id不能为空");
        }
        roleMenuService.insertRoleMenuBatch(roleMenuQuery.getRoleMenus(),roleMenuQuery.getRoleId());
    }

    @Override
    public List<RoleVO> listRoleAll() {
        List<RoleEntity> roleEntities = roleMapper.selectList(new LambdaQueryWrapper<>());
        List<RoleVO> list = Convert.toList(RoleVO.class, roleEntities);
        return list;
    }


}
